package com.woniuxy.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.system.UserInfo;
import com.nimbusds.jose.JOSEException;
import com.woniuxy.entity.DTO.ChangePasswordDTO;
import com.woniuxy.entity.DTO.ReceivingOrderDTO;
import com.woniuxy.entity.DTO.SorterDTO;
import com.woniuxy.mapper.SorterMapper;
import com.woniuxy.service.SorterService;
import com.woniuxy.util.JWTUtil;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class SorterServiceImpl implements SorterService {
    @Resource
    private SorterMapper sorterMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public List<SorterDTO> querySorterList() {
        return sorterMapper.querySorterList();
    }

    @Override
    public Integer updateSorterById(Integer id, Integer warehouseId) {
        return sorterMapper.updateSorterById(id,warehouseId);
    }

    @Override
    public List<SorterDTO> querySorterByWarehouseId(Integer warehouseId) {
        return sorterMapper.querySorterByWarehouseId(warehouseId);
    }

    @Override
    public List<SorterDTO> querySorterByName(String sorterName, Integer warehouseId) {
        return sorterMapper.querySorterByName(sorterName,warehouseId);
    }

    @Override
    public void sorterUnbind(Integer id) {
        sorterMapper.sorterUnbind(id);
    }

    @Override
    public boolean register(SorterDTO sorterDTO)  {
        boolean b = sorterMapper.querySorterByAccount(sorterDTO.getSorterAccount());
        if(b){
            return false;
        }
        String password = SecureUtil.md5(sorterDTO.getSorterPassword() + "1a2b3c");
        sorterDTO.setSorterPassword(password);
        return sorterMapper.register(sorterDTO);
    }

    @Override
    public ResponseData login(SorterDTO sorterDTO) throws JOSEException {
        // 参数校验
        if (sorterDTO == null || sorterDTO.getSorterAccount() == null || sorterDTO.getSorterPassword() == null) {
            return ResponseData.fail(ResponseEnum.LOGIN_FAIL);
        }
        String sorterAccount = sorterDTO.getSorterAccount();
        String md5Password = SecureUtil.md5(sorterDTO.getSorterPassword() + "1a2b3c");
        SorterDTO userInfo = sorterMapper.queryPasswordByAccount(sorterAccount);
        if (userInfo.getSorterPassword() == null) {
            return ResponseData.fail(ResponseEnum.LOGIN_FAIL);
        }
        if (userInfo.getSorterPassword().equals(md5Password)) {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("id", userInfo.getId());
            hashMap.put("account", userInfo.getSorterAccount());
            String jwt = JWTUtil.createJWT(hashMap);
            stringRedisTemplate.opsForValue().set("sorterJWT:" + userInfo.getId(), jwt, 60000, TimeUnit.SECONDS);
            return ResponseData.success(jwt);
        } else {
            return ResponseData.fail(ResponseEnum.LOGIN_FAIL);
        }
    }

    @Override
    public boolean querySorterByAccount (String sorterAccount){
        return sorterMapper.querySorterByAccount(sorterAccount);
    }

    @Override
    public SorterDTO queryProfileByAccount (String sorterName){
        return sorterMapper.queryProfileByAccount(sorterName);
    }

    @Override
    public boolean updatePasswordByName (ChangePasswordDTO changePasswordDTO){
        System.out.println(changePasswordDTO);
        if (changePasswordDTO.getAccount() == null) {
            return false;
        }
        SorterDTO sorterDTO = sorterMapper.queryProfileByAccount(changePasswordDTO.getAccount());
        String s = SecureUtil.md5(changePasswordDTO.getOldPassword() + "1a2b3c");
        if (s.equals(sorterDTO.getSorterPassword())) {
            String s1 = SecureUtil.md5(changePasswordDTO.getNewPassword() + "1a2b3c");
            return sorterMapper.updatePasswordByAccount(changePasswordDTO.getAccount(), s1);
        }
        return false;
    }

    @Override
    public List<ReceivingOrderDTO> queryOrdersByWarehouseId (Integer warehouseId){
        return sorterMapper.queryOrdersByWarehouseId(warehouseId);
    }

    @Override
    public boolean updateSorting(Integer sorterId,List<String> orderIds) {
        for (String orderId : orderIds) {
            sorterMapper.updateSorting(sorterId,orderId);
        }
        return true;
    }

    @Override
    public List<ReceivingOrderDTO> queryOrdersBySorterId(Integer sorterId) {
        return sorterMapper.queryOrdersBySorterId(sorterId);
    }

    @Override
    public boolean updateSortingStatus(String orderId) {
        return sorterMapper.updateSortingStatus(orderId);
    }

    @Override
    public List<ReceivingOrderDTO> queryOrderGoods(String orderId) {
        return sorterMapper.queryOrderGoods(orderId);
    }

    @Override
    public Integer getDailyPerformance(Integer sorterId) {
        return sorterMapper.getDailyPerformance(sorterId);
    }

    @Override
    public Integer getMonthlyPerformance(Integer sorterId) {
        return sorterMapper.getMonthlyPerformance(sorterId);
    }

    @Override
    public void imgLoad(Integer id, String imgSrc) {
        sorterMapper.imgLoad(id,imgSrc);
    }


}
